<style lang="less">
    .ivu-select-dropdown {
        z-index: 10086;
    }
</style>
<template>
    <Drawer :closable="false" placement="right" v-model="drawerVisible" width="45%" :title="drawerTitle">
        <Row>
            <Form ref="form" :model="form" :label-width="100" :rules="formValidate" style="position:relative">
                <FormItem label="考试时间" prop="examTime">
                    <DatePicker type="datetimerange" format="yyyy-MM-dd HH:mm:ss"  @on-change="dataChange" :value="[form.examTime,form.examEndTime]" style="width:300px" placeholder="请输入考试时间"></DatePicker>
                </FormItem>
                <FormItem label="报名时间" prop="examTime">
                    <DatePicker type="datetimerange" format="yyyy-MM-dd HH:mm:ss"  @on-change="selectDateRange" :value="[form.applyBeginDate,form.applyEndDate]" style="width:300px" placeholder="请输入报名时间"></DatePicker>
                </FormItem>
                <FormItem label="缴费截止时间" prop="payEndDate">
                    <DatePicker type="datetime" format="yyyy-MM-dd HH:mm:ss"  @on-change="payEndChange" :value="form.payEndDate" style="width:300px" placeholder="请输入缴费截止时间"></DatePicker>
                </FormItem>
                <FormItem label="退考截止时间" prop="quitDate">
                    <DatePicker type="datetime" format="yyyy-MM-dd HH:mm:ss"  @on-change="quitChange" :value="form.quitDate" style="width:300px" placeholder="请输入缴费截止时间"></DatePicker>
                </FormItem>
                <FormItem label="code值" prop="examinationCode">
                    <Input :value="form.examinationCode" style="width:600px"/>
                </FormItem>
                <FormItem label="考试说明" prop="remark">
                    <div ref="editor"></div>
                </FormItem>
                <Form-item label class="br">
                    <Button @click="editHTML" icon="ios-create-outline">编辑HTML代码</Button>
                    <Button @click="fullscreenModal=true" icon="md-eye">预览</Button>
                </Form-item>
                <Form-item class="br">
                    <Button type="primary" :loading="submitLoading" @click="handelSubmit" style="width:100px">提交</Button>
                    <Button @click="handelCancel">取消</Button>
                </Form-item>
                <Spin size="large" fix v-if="loading"></Spin>
            </Form>
        </Row>
        <!-- html编辑 -->
        <Modal title="编辑html代码" v-model="showHTMLModal" :mask-closable="false" :width="900" :fullscreen="full">
            <Input v-if="!full" v-model="dataEdit" :autosize="true" type="textarea" style="max-height:60vh;overflow:auto"/>
            <Input v-if="full" v-model="dataEdit" :autosize="true" type="textarea"/>
            <div slot="footer">
                <Button @click="full=!full" icon="md-expand">全屏开/关</Button>
                <Button @click="editHTMLOk" type="primary" icon="md-checkmark-circle-outline">确定保存</Button>
            </div>
        </Modal>
        <Modal title="预览" v-model="fullscreenModal" fullscreen>
            <div v-html="form.remark">{{form.remark}}</div>
            <div slot="footer">
                <Button @click="fullscreenModal=false">关闭</Button>
            </div>
        </Modal>
    </Drawer>
</template>
<script>
    import {uploadFile} from "@/api/index";
    import {examinationEdit} from "@/api/bus";
    import E from "wangeditor";
    // 表情包配置 自定义表情可在该js文件中统一修改
    import {sina} from "@/libs/emoji";

    let editor = null;
    var xss = require("xss");
    export default {
        name: "examination-drawer",
        components: {
        },
        data() {
            return {
                type: 0,
                loading: false, // 表单加载状态
                drawerTitle: "编辑考试安排",
                drawerVisible: false,
                form: {
                    remark: '',
                    examTime: '',
                    applyBeginDate: '',
                    applyEndDate: '',
                    payEndDate: '',
                    quitDate: '',
                    certificateId: '',
                    examinationCode: '',
                    examEndTime: ''
                },
                formValidate: {
                    examTime: [{required: true, message: "考试时间不能为空", trigger: "blur"}],
                    applyBeginDate: [{required: true, message: "报名时间不能为空", trigger: "blur"}],
                    payEndDate: [{required: true, message: "缴费截止时间不能为空", trigger: "blur"}],
                    quitDate: [{required: true, message: "退考截止时间不能为空", trigger: "blur"}],
                    remark: [{required: true, message: "考试说明不能为空", trigger: "blur"}],
                    examinationCode: [{required: true, message: "考试安排code值不能为空", trigger: "blur"}]
                },
                submitLoading: false, // 添加或编辑提交状态
                dataEdit: "", // 编辑数据
                showHTMLModal: false, // 显示html
                full: false, // html全屏开关
                fullscreenModal: false// 显示全屏预览
            };
        },
        methods: {
            init() {
                this.initEditor();
            },
            initEditor() {
                let that = this;
                editor = new E(this.$refs.editor);
                editor.customConfig.onchange = html => {
                    this.form.remark = html;
                };
                editor.customConfig.uploadImgServer = uploadFile;
                editor.customConfig.uploadImgHeaders = {
                    Authorization: that.getStore("accessToken")
                };
                editor.customConfig.uploadFileName = "file";
                editor.customConfig.uploadImgHooks = {
                    before: function (xhr, editor, files) {
                    },
                    success: function (xhr, editor, result) {
                    },
                    fail: function (xhr, editor, result) {
                        that.$Message.error("上传图片失败");
                    },
                    error: function (xhr, editor) {
                        that.$Message.error("上传图片出错");
                    },
                    timeout: function (xhr, editor) {
                        that.$Message.error("上传图片超时");
                    },
                    customInsert: function (insertImg, result, editor) {
                        if (result.status === true) {
                            let url = result.data.fileUrl;
                            insertImg(url);
                            that.$Message.success("上传图片成功");
                        } else {
                            that.$Message.error(result.info);
                        }
                    }
                };
                editor.customConfig.fontNames = ["微软雅黑", "宋体", "黑体", "Arial"];
                editor.customConfig.emotions = [
                    {
                        title: "新浪",
                        type: "image",
                        content: sina
                    }
                ];
                editor.create();
            },
            editHTML() {
                this.dataEdit = this.form.remark;
                this.showHTMLModal = true;
            },
            editHTMLOk() {
                editor.txt.html(this.dataEdit);
                this.form.remark = xss(this.dataEdit);
                this.showHTMLModal = false;
            },
            getForm(){
                this.form.id ='';
                this.form.certificateId = ''
                this.form.examTime = ''
                this.form.applyBeginDate = ''
                this.form.applyEndDate = ''
                this.form.payEndDate = ''
                this.form.quitDate = ''
                this.form.remark = ''
                this.examinationCode = ''
                editor.txt.html('');
            },
            showDrawer(params) {
                if(params.id){
                    this.drawerTitle="编辑考试安排";
                    this.form.id = params.id;
                    this.form.certificateId = params.certificateId
                    this.form.examTime = params.examTime
                    this.form.examEndTime = params.examEndTime
                    this.form.applyBeginDate = params.applyBeginDate
                    this.form.applyEndDate = params.applyEndDate
                    this.form.payEndDate = params.payEndDate
                    this.form.quitDate = params.quitDate
                    this.form.remark = params.remark
                    this.form.examinationCode = params.examinationCode
                    editor.txt.html(params.remark);
                }else{
                    this.getForm()
                    this.drawerTitle="新增考试安排";
                    this.form.certificateId = params.certificateId
                }
                this.drawerVisible = true;
            },
            dataChange(v){
                if (v) {
                    this.form.examTime = v[0];
                    this.form.examEndTime = v[1];
                }
            },
            payEndChange(data1,data2){
                this.form.payEndDate = data1
            },
            quitChange(data1,data2){
                this.form.quitDate = data1
            },
            selectDateRange(v) {
                if (v) {
                    this.form.applyBeginDate = v[0];
                    this.form.applyEndDate = v[1];
                }
            },
            handelSubmit() {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        this.submitLoading = true;
                        // 编辑
                        examinationEdit(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.status === true) {
                                this.$Message.success("操作成功");
                                this.$parent.getDataList();
                                this.drawerVisible = false;
                            }
                        });
                    }
                });
            },
            handelCancel() {
                this.drawerVisible = false;
            }
        },
        mounted() {
            this.init();
        },
        watch: {

        }
    };
</script>